package com.amikomgamedev.panjatpinang.database;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.amikomgamedev.panjatpinang.Utils;

/*
 * AndEngineGamePanjatPinang
 * ScoreSVC.java
 * Pratama Wijaya
 * set.mnemonix@gmail.com
 * 1:15:18 PM , Aug 8, 2012
 */

public class ScoreSVC {
	private final String	URL	= "http://www.amikomgamedev.com/ppwebsv/ppWebService.php";

	public boolean sendScore(String nama, float score) {
		String NAMESPACE = "urn:ppWebServices";
		String METHOD_NAME = "sendScore";
		String SOAP_ACTION = "urn:ppWebServices/sendScore";

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("nama", nama);
		request.addProperty("score", score);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			String res = "";

			try {
				res = (String) envelope.getResponse();
				if (res.equalsIgnoreCase("1")) {
					return true;
				}
			} catch (ClassCastException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return false;
	}

	public String[][] getTopTen() {
		String kembalian[][] = null;
		String NAMESPACE	= "urn:ppWebServices";
    	String METHOD_NAME	= "getTopTen";
    	String SOAP_ACTION	= "urn:ppWebServices/getTopTen";

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			String res = "";
			try {
				res = (String) envelope.getResponse();
				Utils.TRACE("isi RES" +res);
			} catch (ClassCastException e) {
				// TODO: handle exception
				e.printStackTrace();
			}

			JSONObject json = new JSONObject(res);
			JSONArray jsonArray = json.getJSONArray("data");
			kembalian = new String[jsonArray.length()][2];

			for (int i = 0; i < jsonArray.length(); i++) {
				JSONArray JobRec = jsonArray.getJSONArray(i);
				String namaDapat = JobRec.get(0).toString();
				String scoreDapat = JobRec.get(1).toString();
				kembalian[i][0] = namaDapat;
				kembalian[i][1] = scoreDapat;
			}
			return kembalian;

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return kembalian;

	}

}
